package com.ys.service.finance.impl;

import com.ys.entity.finance.TcwAccountCountForWanli;
import com.ys.entity.finance.TcwDepositPlatform;
import com.ys.entity.finance.TcwWanliFinance;
import com.ys.mapper.finance.TcwAccountCountForWanliMapper;
import com.ys.mapper.finance.TcwWanliFinanceMapper;
import com.ys.service.finance.TcwAccountCountForWanliService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 万里汇账号交易流水
 */
@Service("tcwAccountCountForWanliService")
public class TcwAccountCountForWanliServiceImpl implements TcwAccountCountForWanliService {
	private static final Logger logger = LoggerFactory.getLogger(TcwAccountCountForWanliServiceImpl.class);

	@Autowired
	private TcwAccountCountForWanliMapper tcwAccountCountForWanliMapper;

	@Autowired
	private TcwWanliFinanceMapper tcwWanliFinanceMapper;

	@Autowired
	private CheckUtil checkUtil;


	/**
	 * 判断资金是否充足；新增流水、并修改对应万里汇资金信息
	 * @param isCheckCapital 是否判断资金是否充足，是true、否false（若为否，并扣除资金可能为负）
	 * @param depositPlatformCode 收款平台账号管理Code(TCW_DepositPlatform)
	 * @param exchangeRateCode 货币代码
	 * @param documentType 单据类型（见枚举类：TcwAccountCountForWanliEnum）
	 * @param accountCount 金额
	 * @param remark 备注
	 * @param documentTypeBid 来源单据
	 * @return R SUCCESS("成功", 0), FAILED("失败", 1);  失败原因：R.getMsg()
	 * @throws Exception
	 */
	@Override
	public R add(boolean isCheckCapital,String depositPlatformCode, String exchangeRateCode, int documentType,String documentTypeBid, BigDecimal accountCount,String remark) throws Exception{
		logger.info("新增流水传参：depositPlatformCode="+depositPlatformCode+",exchangeRateCode="+exchangeRateCode+",documentType="+documentType+",accountCount="+accountCount+",remark="+remark);

		//获取万里汇账号资金信息
		TcwWanliFinance tcwWanliFinance = new TcwWanliFinance().setDepositPlatformCode(depositPlatformCode).setExchangeRateCode(exchangeRateCode).queryFirst();
		if(tcwWanliFinance!=null){
			//判断资金是否充足
			BigDecimal systemSumCount = tcwWanliFinance.getSystemSum().add(accountCount);
			if(isCheckCapital && systemSumCount.compareTo(BigDecimal.ZERO)<0){
				return R.error("该万里汇账号【账号编码："+tcwWanliFinance.getDepositPlatformCode()+"】资金不足");
			}
			//获取所属公司信息
			TcwDepositPlatform tcwDepositPlatform = new TcwDepositPlatform().setCode(depositPlatformCode).queryFirst();
			String companyVid = "";
			if(tcwDepositPlatform!=null){
				companyVid = tcwDepositPlatform.getCompanyVid();
			}

			//新增流水
			TcwAccountCountForWanli entity = new TcwAccountCountForWanli();
			entity.setDepositPlatformCode(depositPlatformCode);//收款平台账号管理Mid
			if(companyVid!=null)entity.setCompanyVid(companyVid);//公司编码
			if(exchangeRateCode!=null)entity.setExchangeRateCode(exchangeRateCode);//货币代码
			entity.setDocumentType(documentType);//单据类型（0万里汇提款单、1万里汇CNY付款单、2预付款退回申请、3预付款退回）
			entity.setDocumentBid(documentTypeBid);
			entity.setAccountCount(accountCount);//金额
			if(remark!=null)entity.setRemark(remark);//备注
			entity.setStatus(1);
			tcwAccountCountForWanliMapper.insert(entity);

			//修改对应万里汇资金信息
			tcwWanliFinance.setSystemSum(systemSumCount);
			tcwWanliFinanceMapper.update(tcwWanliFinance);
		}else {
			return R.error("未查询到万里汇账号资金信息");
		}
		return R.ok();
	}




}
